package com.starsoft.quant.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.base.bean.BaseAcount;
import com.starsoft.frame.base.bean.UserContext;
import com.starsoft.frame.base.service.AcountService;
import com.starsoft.frame.base.service.UserService;
import com.starsoft.quant.bean.QuantAcount;
import com.starsoft.quant.bean.QuantModel;
import com.starsoft.quant.bean.QuantModelParam;

@RestController
@RequestMapping("/quant/report")
public class  CmpReportController extends QuantReportControllerBase{

	@Autowired
	AcountService acountService;
	
	@RequestMapping("/acounts")
	public List<Map<String, Object>> pageParent(ServletRequest request){
		UserContext context = UserService.getUserContext();
		List<BaseAcount> acounts = acountService.getAcount(context.getUser());
		
		List<Map<String, Object>> result = new ArrayList<>();
		for (BaseAcount acount : acounts) {
			QuantAcount quantAcount = quantAcountService.getAcount(acount.getAcountId());
			if(quantAcount==null) continue;
			QuantModel quantModel = quantAcountService.getModel(acount.getAcountId());
			if(quantModel==null) continue;

			Map<String, Object> config = new HashMap<String, Object>();
			config.put("acountId", acount.getAcountId());
			config.put("acountName", acount.getAcountName());
			config.put("acountDesc", acount.getAcountDesc());
			config.put("modelCode", quantModel.getCode());
			config.put("modelName", quantModel.getName());
			if(acount.getParentId()!=null){
				BaseAcount pAcount = acountService.getAcount(acount.getParentId());
				config.put("parentName", pAcount.getAcountName());
			}
			result.add(config);
		}
		return result;
	}
	
	
	@RequestMapping("/cmpreport.json")
	public Map<String, Object> getCmpReport(ServletRequest request) throws Exception {
		List<String> message = new ArrayList<String>();
		List<QuantAcount> acounts = getQuantAcount(request, message);

		Date reportDate = getReportDate(request);
		boolean isRefresh = "true".equalsIgnoreCase(request.getParameter("isRefresh"));

		Map<Integer, QuantModelParam> modelParamMap = new HashMap<>();
		List<Map<String, Object>> modelconfig = new ArrayList<>();
		List<Map<String, Object>> quantRisk = new ArrayList<>();
		for (QuantAcount quantAcount : acounts) {
			Date reportDateAcount = reportDate;
			if (quantAcount.getEndDate() != null && reportDateAcount.after(quantAcount.getEndDate())) {
				reportDateAcount = quantAcount.getEndDate();
			}
			if (isRefresh) {
				reportService.updateAnalysis(quantAcount.getAcountId(), reportDateAcount);
			}
			List<QuantModelParam> modelParams = modelParamService.getModelParams(quantAcount.getModelCode());
			for(QuantModelParam modelParam : modelParams){
				modelParamMap.put(modelParam.getId(), modelParam);
			}
			modelconfig.add(loadConfig(quantAcount, reportDateAcount));
			quantRisk.add(loadRisk(quantAcount.getAcountId(), reportDateAcount));
		}
		Map<String, Object> model = new HashMap<>();
		model.put("modelconfig", modelconfig);
		model.put("risk", quantRisk);
		model.put("modelParams", modelParamMap.values());
		model.put("profit", loadProfit(acounts, null, reportDate));
		return model;
	}
	
	@RequestMapping(value = "/cmpprofit")
	public Map<String, Object> getCmpProfit(ServletRequest request) throws Exception {
		Map<String, Object> model = new HashMap<>();
		List<String> message = new ArrayList<String>();
		List<QuantAcount> acounts = getQuantAcount(request, message);
		Date reportDate = getReportDate(request);
		Date beginDate = getBeginDate(request, reportDate, acounts);
		model.put("profit", loadProfit(acounts, beginDate, reportDate));
		return model;
	}

}
